//
//  SISSUploadEntrustContractViewController.m
//  SISSAPP
//
//  Created by 袁应荣 on 2019/7/10.
//  Copyright © 2019 优房网. All rights reserved.
//

#import "SISSUploadEntrustContractViewController.h"
#import "SISSNetworkingManager+HouseProgress.h"
#import "SISSNetworkingManager+PassengerProgress.h"
#import "SISSNetworkingManager+RentOutProgress.h"
#import "SISSNetworkingManager+TenantProgress.h"
#import "SISSNetworkingManager+Progress.h"

@interface SISSUploadEntrustContractViewController ()

@end

@implementation SISSUploadEntrustContractViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    if (self.popBackClassType == SISSProgressPopBackClassTypeHouse) {
        self.title = @"二手房卖方委托合同";
    } else if (self.popBackClassType == SISSProgressPopBackClassTypePassenger) {
        self.title = @"二手房买方委托合同";
    } else if (self.popBackClassType == SISSProgressPopBackClassTypeRentOut) {
        self.title = @"物业租出租方委托协议";
        self.contractNoView.leftStr = @"*协议编号";
        self.contractNoView.rightPlaceStr = @"请输入协议编号";
        self.contractDateChooseView.leftStr = @"*协议截止日期";
        self.contractDateChooseView.rightPlaceStr = @"请选择协议截止日期";
        self.uploadImageView.titleStr = @"协议信息";
    } else if (self.popBackClassType == SISSProgressPopBackClassTypeTenant) {
        self.title = @"物业租赁承租方委托协议";
        self.contractNoView.leftStr = @"*协议编号";
        self.contractNoView.rightPlaceStr = @"请输入协议编号";
        self.contractDateChooseView.leftStr = @"*协议截止日期";
        self.contractDateChooseView.rightPlaceStr = @"请选择协议截止日期";
        self.uploadImageView.titleStr = @"协议信息";
    }
    if (self.entrustContractModel) {
        self.contractNoView.rightTextField.text = self.entrustContractModel.contractNum;
        self.contractDateChooseView.rightStr = [NSDate yyr_stringToString:self.entrustContractModel.endTime];
        self.uploadImageView.imageUrlArr = self.entrustContractModel.image.mutableCopy;
        self.remarkView.rightTextField.text = self.entrustContractModel.otherdesc;
    }
}


- (void)confirmButtonClickAction {
    [super confirmButtonClickAction];
    if (self.contractNoView.rightTextField.text.length == 0) {
        [SISSTools showSVErrorTitle:@"请输入合同编号" andTime:2 andColor:YES];
        return;
    }
    if (self.contractDateChooseView.rightStr.length == 0) {
        [SISSTools showSVErrorTitle:@"请选择合同截止日期" andTime:2 andColor:YES];
        return;
    }
    if (!self.entrustContractModel && self.uploadImageView.images.count == 0) {
        [SISSTools showSVErrorTitle:@"请添加合同文本图片" andTime:2 andColor:YES];
        return;
    }
    
    NSString *otherdesc = self.remarkView.rightTextField.text ? self.remarkView.rightTextField.text : @"";
    NSDictionary *parameters = @{
                                 @"abpId" : self.abpId,
                                 @"contractNum" : self.contractNoView.rightTextField.text,
                                 @"endTime" : self.contractDateChooseView.rightStr,
                                 @"otherdesc" : otherdesc,
                                 };
    if (self.entrustContractModel) {
        NSDictionary *dict = @{@"id" : self.entrustContractModel.ID,};
        NSMutableDictionary *params = [NSMutableDictionary dictionaryWithDictionary:dict];
        [params addEntriesFromDictionary:parameters];
        
        [kNetworkManager updateEntrustContractWithParameters:params images:self.uploadImageView.images success:^(id responseObject) {
            if ([responseObject[@"code"] integerValue] == 200) {
                [self pushToSuccessViewWithPopBackClassType:self.popBackClassType];
            }
        } failed:^(NSError *error) {
            NSLog(@"%@",error);
        }];
    } else {
        
        if (self.popBackClassType == SISSProgressPopBackClassTypeHouse) {
            [kNetworkManager insertHouseEntrustContractWithParameters:parameters images:self.uploadImageView.images success:^(id responseObject) {
                if ([responseObject[@"code"] integerValue] == 200) {
                    [self pushToSuccessViewWithPopBackClassType:self.popBackClassType];
                }
            } failed:^(NSError *error) {
                NSLog(@"%@",error);
            }];
        } else if (self.popBackClassType == SISSProgressPopBackClassTypePassenger) {
            [kNetworkManager insertPassengerEntrustContractWithParameters:parameters images:self.uploadImageView.images success:^(id responseObject) {
                if ([responseObject[@"code"] integerValue] == 200) {
                    [self pushToSuccessViewWithPopBackClassType:self.popBackClassType];
                }
            } failed:^(NSError *error) {
                NSLog(@"%@",error);
            }];
        } else if (self.popBackClassType == SISSProgressPopBackClassTypeRentOut) {
            [kNetworkManager insertLeaserEntrustContractWithParameters:parameters imagesArray:self.uploadImageView.images successBlock:^(id responseObject) {
                if ([responseObject[@"code"] integerValue] == 200) {
                    [self pushToSuccessViewWithPopBackClassType:self.popBackClassType];
                }
            } failed:^(NSError *error) {
                NSLog(@"%@",error);
            }];
            
        } else if (self.popBackClassType == SISSProgressPopBackClassTypeTenant) {
            [kNetworkManager insertTenantEntrustContractWithParameters:parameters imagesArray:self.uploadImageView.images successBlock:^(id responseObject) {
                if ([responseObject[@"code"] integerValue] == 200) {
                    [self pushToSuccessViewWithPopBackClassType:self.popBackClassType];
                }
            } failed:^(NSError *error) {
                NSLog(@"%@",error);
            }];
        }
    }
    
}

@end
